﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class AdjustingDuctTerminalPosition : System.Windows.Forms.Form
	{
		public AdjustingDuctTerminalPosition()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{1E2070D4-01CD-4FAF-B8F6-E213D1EC0092}", null);
			base.Icon = Resource.YArchitechIcon;
		}

		private void AdjustingDuctTerminalPosition_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.GetUserData();
		}

		private void win_deactivate(object sender, EventArgs e)
		{
		}

		private void GetUserData()
		{
			string[] userDataOfTerminalDistanceText = this.userSetting.GetUserDataOfTerminalDistanceText();
			if (userDataOfTerminalDistanceText != null)
			{
				this.tbhori.Text = userDataOfTerminalDistanceText[0];
				this.tbvert.Text = userDataOfTerminalDistanceText[1];
				return;
			}
			this.tbhori.Text = "1500";
			this.tbvert.Text = "500";
		}

		private void WriteUserData()
		{
			string[] texts = new string[]
			{
				this.tbhori.Text,
				this.tbvert.Text
			};
			this.userSetting.WriteUserDataOfTerminalDistanceText(texts);
		}

		private void tbvert_TextChanged(object sender, EventArgs e)
		{
		}

		private void tbhori_TextChanged(object sender, EventArgs e)
		{
		}

		private void ConnDuctFrom_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void AdjustingDuctTerminalPosition_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			AdjustingDuctTerminalPosition.SendMessage(AdjustingDuctTerminalCommand.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			AdjustingDuctTerminalPosition.keybd_event((byte)bVk, 0, dwFlags, 0);
			AdjustingDuctTerminalPosition.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void AdjustingDuctTerminalPosition_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			if (this.tbhori.Text.Trim() != "" && this.tbvert.Text.Trim() != "")
			{
				this.rheight = double.Parse(this.tbvert.Text);
				this.rwith = double.Parse(this.tbhori.Text);
				this.WriteUserData();
			}
		}

		public double rwith = 1500.0;

		public double rheight = 500.0;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private ControlInfoManager m_thisManger;
	}
}
